#![allow(unused_macros, unused_attributes, dead_code)]

use crate::util::conf::config;
use serde::{Deserialize};
use std::path::Path;

/// load config file to struct
/// supported formats
/// [x] yaml
/// [x] json
/// [x] toml
/// [x] http
///
/// example
///
/// ```rust
///#[derive(Debug, Deserialize)]
/// pub struct Config {
///     pub log: i32,
///     pub server: String,
///     pub stream: String,
/// }
/// let conf:Config = load_config("./src/util/conf/config.toml").unwrap();
/// println!("{:?}",conf)
/// ```
#[allow(unused_unsafe)]
pub fn load_config<T:for<'a> Deserialize<'a>>(path:impl AsRef<Path>)->anyhow::Result<T>{
    let path:&'static str = unsafe {
        let s = path.as_ref().to_string_lossy().to_string();
        Box::leak(Box::new(s))
    };
    config::load_config_from_path(config::File::new(path))
}


#[cfg(test)]
mod test{
    use serde::{Deserialize};
    use crate::load_config;

    #[derive(Debug, Deserialize)]
    pub struct Config {
        pub log: i32,
        pub server: String,
        pub stream: String,
    }

    #[test]
    fn load_config_test(){
        let conf:Config = load_config("./src/util/conf/config.toml").unwrap();
         println!("{:?}",conf)
    }
}